#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define KEY 6
char *keyword[KEY] = {"for", "if", "then", "else", "while", "do"};
char id[1024];
int num;
int token;

int lexical(FILE *fp) {
	char x = getc(fp);

		num = 0;
			strcpy(id, "\0");

				//process x
					//process each regex here
						//store the result in global variables id, num, or token
							//in case of backtrack, use
								//ungetc(x, fp);

									if ((x>= 'a'&&x <= 'z') ||( x>= 'A'&&x <= 'Z')||x=='_')
										{
												int i=0;
														id[i]=x;
																while(id[i]!=-1){
																			fp++;
																						x=getc(fp);
																									if((x>= 'a'&&x <= 'z') ||( x>= 'A'&&x <= 'Z')||x=='_'||(x>= '0'&&x <= '9'))
																												{
																															   i++;
																															   			   id[i]=x;
																																		   		    }
																																				    		    else{
																																						    		    	ungetc(x,fp);
																																											    	break;
																																														    }
																																														    			
																																																			}	

																																																					token=10;
																																																							fp++;
																																																								}
																																																									
																																																										else if (x>= '0'&&x <= '9')
																																																											{
																																																													num=x;
																																																															while(x!=-1){
																																																																	
																																																																			fp++;
																																																																					x=getc(fp);
																																																																							if(x>= '0'&&x <= '9')
																																																																									{
																																																																												num=num*10+x;
																																																																														}
																																																																																else{
																																																																																			ungetc(x,fp);
																																																																																						break;
																																																																																								}
																																																																																									    }
																																																																																									    		token=11;
																																																																																													fp++;
																																																																																														}
																																																																																															
																																																																																																else if (x== keyword[1])
																																																																																																	{
																																																																																																			token=1;
																																																																																																					fp++;
																																																																																																						}
																																																																																																							else if (x == keyword[2])
																																																																																																								{
																																																																																																										token=2;
																																																																																																											    fp++;
																																																																																																											    	}
																																																																																																													else if (x == keyword[3])
																																																																																																														{
																																																																																																															    token=3;
																																																																																																															            fp++;
																																																																																																																    	}
																																																																																																																		else if (x == keyword[4])
																																																																																																																			{
																																																																																																																					token=4;
																																																																																																																							fp++;
																																																																																																																								}
																																																																																																																									else if (x == keyword[5])
																																																																																																																										{
																																																																																																																												token=5;
																																																																																																																														fp++;
																																																																																																																															}
																																																																																																																																else if (x == keyword[6])
																																																																																																																																	{
																																																																																																																																			token=6;
																																																																																																																																					fp++;
																																																																																																																																						}
																																																																																																																																							
																																																																																																																																								else if (x == '+')
																																																																																																																																									{
																																																																																																																																											token=13;
																																																																																																																																													id[0]=x;
																																																																																																																																															fp++;		
																																																																																																																																																}

																																																																																																																																																	else if (x == '-'){
																																																																																																																																																			token=14;
																																																																																																																																																					id[0]=x;
																																																																																																																																																							fp++;
																																																																																																																																																								}
																																																																																																																																																									else if (x == '*'){
																																																																																																																																																											token=15;
																																																																																																																																																													id[0]=x;
																																																																																																																																																															fp++;
																																																																																																																																																																}
																																																																																																																																																																	else if (x == '/'){
																																																																																																																																																																			token=16;
																																																																																																																																																																					id[0]=x;
																																																																																																																																																																							fp++;
																																																																																																																																																																								}
																																																																																																																																																																									else if (x == ':'){
																																																																																																																																																																											id[0]=x;
																																																																																																																																																																													fp++;
																																																																																																																																																																															x=getc(fp);
																																																																																																																																																																																	if(x!='=')
																																																																																																																																																																																			{
																																																																																																																																																																																						token=17;
																																																																																																																																																																																									ungetc(x,fp);
																																																																																																																																																																																											}
																																																																																																																																																																																													else
																																																																																																																																																																																															{
																																																																																																																																																																																																		token=18;
																																																																																																																																																																																																					id[1]=x;
																																																																																																																																																																																																							}
																																																																																																																																																																																																									fp++;
																																																																																																																																																																																																										}

																																																																																																																																																																																																											else if (x == '<')
																																																																																																																																																																																																												{
																																																																																																																																																																																																														id[0]=x;
																																																																																																																																																																																																																fp++;
																																																																																																																																																																																																																		x=getc(fp);
																																																																																																																																																																																																																				if(x=='>')
																																																																																																																																																																																																																						{
																																																																																																																																																																																																																									token=21;
																																																																																																																																																																																																																												id[1]=x;
																																																																																																																																																																																																																														}
																																																																																																																																																																																																																																else if(x=='=')
																																																																																																																																																																																																																																		{
																																																																																																																																																																																																																																					token=22;
																																																																																																																																																																																																																																								id[1]=x;
																																																																																																																																																																																																																																										}
																																																																																																																																																																																																																																												else {
																																																																																																																																																																																																																																															token=20;
																																																																																																																																																																																																																																																		ungetc(x,fp);
																																																																																																																																																																																																																																																			    }
																																																																																																																																																																																																																																																			    	fp++;
																																																																																																																																																																																																																																																					}
																																																																																																																																																																																																																																																						else if (x == '>')
																																																																																																																																																																																																																																																							{
																																																																																																																																																																																																																																																									id[0]=x;
																																																																																																																																																																																																																																																											fp++;
																																																																																																																																																																																																																																																													x=getc(fp);
																																																																																																																																																																																																																																																															if(x=='=')
																																																																																																																																																																																																																																																																	{
																																																																																																																																																																																																																																																																				token=24;
																																																																																																																																																																																																																																																																							id[1]=x;
																																																																																																																																																																																																																																																																									}
																																																																																																																																																																																																																																																																											else {
																																																																																																																																																																																																																																																																														token=23;
																																																																																																																																																																																																																																																																																	ungetc(x,fp);
																																																																																																																																																																																																																																																																																		    }
																																																																																																																																																																																																																																																																																		    	fp++;
																																																																																																																																																																																																																																																																																				}
																																																																																																																																																																																																																																																																																					else if (x == '='){
																																																																																																																																																																																																																																																																																							token=25;
																																																																																																																																																																																																																																																																																									id[0]=x;
																																																																																																																																																																																																																																																																																											fp++;
																																																																																																																																																																																																																																																																																												}
																																																																																																																																																																																																																																																																																													else if (x == ';'){
																																																																																																																																																																																																																																																																																															token=26;
																																																																																																																																																																																																																																																																																																	id[0]=x;
																																																																																																																																																																																																																																																																																																			fp++;
																																																																																																																																																																																																																																																																																																				}
																																																																																																																																																																																																																																																																																																					else if (x == '('){
																																																																																																																																																																																																																																																																																																							token=27;
																																																																																																																																																																																																																																																																																																									id[0]=x;
																																																																																																																																																																																																																																																																																																											fp++;
																																																																																																																																																																																																																																																																																																												}
																																																																																																																																																																																																																																																																																																													else if (x == ')'){
																																																																																																																																																																																																																																																																																																															token=28;
																																																																																																																																																																																																																																																																																																																	id[0]=x;
																																																																																																																																																																																																																																																																																																																			fp++;
																																																																																																																																																																																																																																																																																																																				}
																																																																																																																																																																																																																																																																																																																					else if (x == '#'){
																																																																																																																																																																																																																																																																																																																							token=0;
																																																																																																																																																																																																																																																																																																																									id[0]=x;
																																																																																																																																																																																																																																																																																																																											fp++;
																																																																																																																																																																																																																																																																																																																												}
																																																																																																																																																																																																																																																																																																																													else
																																																																																																																																																																																																																																																																																																																															token=-1;
																																																																																																																																																																																																																																																																																																																																	return 0;
																																																																																																																																																																																																																																																																																																																																	}

																																																																																																																																																																																																																																																																																																																																	int main(int argc, const char *argv[])
																																																																																																																																																																																																																																																																																																																																	{
																																																																																																																																																																																																																																																																																																																																		if(argc < 2) {
																																																																																																																																																																																																																																																																																																																																				printf("insufficient args\n");
																																																																																																																																																																																																																																																																																																																																						return 1;
																																																																																																																																																																																																																																																																																																																																							}
																																																																																																																																																																																																																																																																																																																																								FILE *fp = fopen(argv[1], "r");
																																																																																																																																																																																																																																																																																																																																									do {
																																																																																																																																																																																																																																																																																																																																											lexical(fp);
																																																																																																																																																																																																																																																																																																																																													switch(token) {
																																																																																																																																																																																																																																																																																																																																																case 10: printf("(id, %s)\n", id); break;
																																																																																																																																																																																																																																																																																																																																																			case 11: printf("(num, %d)\n", num); break;
																																																																																																																																																																																																																																																																																																																																																						case -1: printf("error!\n"); break;
																																																																																																																																																																																																																																																																																																																																																									default: printf("(%d, %s)\n", token, id); 		
																																																																																																																																																																																																																																																																																																																																																											}
																																																																																																																																																																																																																																																																																																																																																												} while(token != 0);
																																																																																																																																																																																																																																																																																																																																																													

																																																																																																																																																																																																																																																																																																																																																														fclose(fp);
																																																																																																																																																																																																																																																																																																																																																															return 0;
																																																																																																																																																																																																																																																																																																																																																															}
